package com.cgtong.android.callback;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.util.DiffUtil;

import com.cgtong.android.bean.ProblemBean;

import java.util.List;

/**
 * 用来判断 新旧Item是否相等
 */
public class DiffCallBack extends DiffUtil.Callback {

    public static final String BUNDLE_IS_USER_ATTENTION = "is_user_attention";
    private static final String TAG = DiffCallBack.class.getSimpleName();
    private List<ProblemBean> mOldList;
    private List<ProblemBean> mNewList;

    public DiffCallBack(List<ProblemBean> oldList,
                        List<ProblemBean> newList) {
        this.mOldList = oldList;
        this.mNewList = newList;
    }

    @Override
    public int getOldListSize() {
        return mOldList != null ? mOldList.size() : 0;
    }

    @Override
    public int getNewListSize() {
        return mNewList != null ? mNewList.size() : 0;
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        return mOldList.get(oldItemPosition).getId() == mNewList.get(newItemPosition).getId();
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        ProblemBean oldMedical = mOldList.get(oldItemPosition);
        ProblemBean newMedical = mNewList.get(newItemPosition);

        //是否关注用户
        if (oldMedical.getIsCreateUserAttention() != newMedical.getIsCreateUserAttention()) {
            return false;
        }
        return true; //默认两个data内容是相同的
    }

    @Nullable
    @Override
    public Bundle getChangePayload(int oldItemPosition, int newItemPosition) {
        ProblemBean oldMedical = mOldList.get(oldItemPosition);
        ProblemBean newMedical = mNewList.get(newItemPosition);
        Bundle bundle = new Bundle();
        //是否关注用户
        if (oldMedical.getIsCreateUserAttention() != newMedical.getIsCreateUserAttention()) {
            bundle.putInt(BUNDLE_IS_USER_ATTENTION, newMedical.getIsCreateUserAttention());
        }
        return bundle;
    }
}